var PdfPreview = new Class({
	"Extends":js.underas.container.ModWindow
	,"_pdf":null
	,"_dtatemp":null
	,"_url_template":null
	,"initialize":function(){
		this.parent("relatorio");
		this.setRevision("$Revision: 1 $");
		this.pdfPreview = new js.underas.core.Component('div','<iframe class="preview-pane-iframe" width="100%" height="650" frameborder="0" style="position:relative;z-index:999" src=""></iframe>');
		this.getEle().addClass("preview-pane");
		this.append(this.pdfPreview);
	}
	,"setReportPreview":function(psrc){
		this.getEle('iframe').set("src",psrc);	
	}
	,"loadPrintLibs":function(){
		if(!window["jsPDF"]){
			Asset.javascript('lib/jspdf/libspdf.min.js', {
				id: 'jspdfscripts',
				onLoad: function(){
					pdfpreview.loadReportPreview(pdfpreview._dtatemp,pdfpreview._url_template);
				}
			});
			return false;
		};
		return true;
	}
	,"loadReportPreview":function(p_dta,p_url_template){	
		this._dtatemp = p_dta;
		this._url_template = p_url_template;		
		if(this.loadPrintLibs()){
			new Request.JSON({url:p_url_template
				,"onSuccess":function(dtaj){
					dtaj["dataSets"][0]["itens"] = pdfpreview._dtatemp;			 	
					//if(!pdfpreview.pdf){
					pdfpreview._pdf = new jsPDF('p','pt','a4');			 		
					//}			 	
					pdfpreview._pdf.setJereport(dtaj);
					pdfpreview.setReportPreview(pdfpreview._pdf.output('datauristring'));
					//liberando a memoria
					p_dta = null;
					dtaj = null; 
					pdfpreview._pdf = null;
					pdfpreview._dtatemp = null;
					pdfpreview._url_template = null;
				}
				,"onFailure":function(xhr){				
					var errotxt = "Unknow Error";
					if (xhr.status == 0) {
						errotxt = 'You are offline!! Please Check Your Network.';
					} else if (xhr.status == 404) {
						errotxt = 'URL not found.';
					} else if (xhr.status == 500) {
						errotxt = 'Internal Server Error.';
					}else{
						errotxt = 'Unknow Error.' + xhr.responseText;
					}				
					console.log(errotxt);
				}
				,"onError":function(text, error){
					console.log(error);
				}
			}).get({"a":"b"});	
		}
	}
});
